package org.twndmv.jpa;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.NonUniqueResultException;
import javax.persistence.Query;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.twndmv.bo.Person;
import org.twndmv.bo.VehicleRegistration;
import org.twndmv.dao.VehicleRegistrationDAO;

public class VehicleRegistrationDAOImpl implements VehicleRegistrationDAO {

	private static Log log = LogFactory.getLog(VehicleRegistrationDAOImpl.class);
	private EntityManager em;
	
	public VehicleRegistrationDAOImpl() {
	}
	
	public VehicleRegistrationDAOImpl(EntityManager eman) {
		this.em = eman;
	}
	
	@Override
	public VehicleRegistration getRegistrationByID(long id) {
		//Query query = em.createQuery("Select a from VehicleRegistration as a where id = " + id);
		VehicleRegistration results = null;
		results = em.find(VehicleRegistration.class, id);
//		try {
//		 results = (VehicleRegistration)query.getSingleResult();
//		} catch (NoResultException nre) {
//		  log.warn("No VehicleRegistration found with id = " + id);	
//		} catch (NonUniqueResultException nure) {
//			log.warn("Two or more VehicleRegistrations have the same id = " + id);
//		}
		return results;
	}

	@Override
	public VehicleRegistration getRegistrationByTag(String tagNo) {
		Query query = em.createQuery("Select a from VehicleRegistration as a where tagNo = " + tagNo);
		VehicleRegistration results = null;
		try {
		 results = (VehicleRegistration)query.getSingleResult();
		} catch (NoResultException nre) {
		  log.warn("No VehicleRegistration found with tagNo = " + tagNo);	
		} catch (NonUniqueResultException nure) {
			log.warn("Two or more VehicleRegistrations have the same tagNo = " + tagNo);
		}
		return results;
	}



	@Override
	public List<VehicleRegistration> getRegistrationsExpiringWithin(
			Date expireDate) {
		Query query = em.createQuery("Select a from VehicleRegistration as a where expiration > " + expireDate.getTime());
		List<VehicleRegistration> resultCars = new ArrayList<VehicleRegistration>();
		for(Object entry : query.getResultList()) {
			VehicleRegistration results = (VehicleRegistration)entry;
			resultCars.add(results);
		}
		return resultCars;
	}

	@Override
	public void createRegistration(VehicleRegistration newCar) {
		em.persist(newCar);
//		em.getTransaction().begin();
//        em.getTransaction().commit();
	}

	@Override
	public boolean deleteRegistration(VehicleRegistration deadCar) {
		em.remove(deadCar);
//		em.getTransaction().begin();
//		Query query = em.createQuery("Delete from VehicleRegistration where id = " + deadCar.getId());
//		int results = query.executeUpdate();
//		em.getTransaction().commit();
		return true;
	}

	@Override
	public int purgeVehicles() {
		int results = 0;
		for(VehicleRegistration car : em.createQuery("select c from VehicleRegistration c", VehicleRegistration.class).getResultList()){
		  em.remove(car);
		  results++;
		}
//		
//		em.getTransaction().begin();
//		Query query = em.createQuery("Delete from VehicleRegistration");
//		int results = query.executeUpdate();
//		em.getTransaction().commit();
		return results;
	}

	@Override
	public void setEntityManager(EntityManager em) {
		this.em = em;
	}

	@Override
	public List<VehicleRegistration> getRegistrationsByMakeModel(String make,
			String model) {
		Query query = em.createQuery("Select a from VehicleRegistration as a where make = '" + make + "' and model = '" + model + "'");
		List<VehicleRegistration> resultCars = new ArrayList<VehicleRegistration>();
		for(Object entry : query.getResultList()) {
			VehicleRegistration results = (VehicleRegistration)entry;
			resultCars.add(results);
		}
		return resultCars;
	}

	@Override
	public List<VehicleRegistration> getRegistrationsByOwner(Person person) {
		Query query = em.createQuery("Select a from VehicleRegistration as a where :person member of a.owners");
		query.setParameter("person", person);
		List<VehicleRegistration> resultCars = new ArrayList<VehicleRegistration>();
		for(Object entry : query.getResultList()) {
			VehicleRegistration results = (VehicleRegistration)entry;
			resultCars.add(results);
		}
		return resultCars;
	}
	
	@Override
	public List<VehicleRegistration> getAllVehicles() {
		List<VehicleRegistration> cars = em.createQuery("select c from VehicleRegistration c", VehicleRegistration.class).getResultList();
		return cars;
	}



}
